package org.hfoss.POCT;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class BuildingActivity extends Activity implements OnClickListener{
	public static final String TAG = "BuildingActivity";
	public Building mBuilding;
	public int mBuilding_id;
	public Cursor audioClips, videos, images;
	public ImageView building_photo;
	public ImageButton launch_audio_1, launch_audio_2, launch_audio_3, launch_video_1,launch_video_2,launch_video_3;
	public TextView building_name, building_description;
	public Button go_back_button; 
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			Intent intent = getIntent();
			setContentView(R.layout.template);
			mBuilding_id = intent.getIntExtra("building_id", -1);
			building_photo = (ImageView) findViewById(R.id.building_photo);
			building_name = (TextView) findViewById(R.id.building_name);
			building_description = (TextView) findViewById(R.id.building_description);
			mBuilding = new Building(this, mBuilding_id);
			building_name.setText(mBuilding.building_name);
			building_description.setText(mBuilding.building_description);
			go_back_button = (Button) findViewById(R.id.go_back_button);
			images = mBuilding.images;
			if (images.getCount() > 0){
				images.moveToFirst();
				String s = images.getString(images.getColumnIndex(MyDBHelper.KEY_IMAGE_URI));
				if (s != null) {
					building_photo.setImageURI(Uri.parse(s));
				}
			}
			images.close();
			videos = mBuilding.videos;
			switch (videos.getCount()){
			case 3:
				launch_video_1 = (ImageButton) findViewById(R.id.launch_video_1);
				launch_video_1.setVisibility(View.VISIBLE);
				launch_video_2 = (ImageButton) findViewById(R.id.launch_video_2);
				launch_video_2.setVisibility(View.VISIBLE);
				launch_video_3 = (ImageButton) findViewById(R.id.launch_video_3);
				launch_video_3.setVisibility(View.VISIBLE);
				break;
			case 2:
				launch_video_1 = (ImageButton) findViewById(R.id.launch_video_1);
				launch_video_1.setVisibility(View.VISIBLE);
				launch_video_2 = (ImageButton) findViewById(R.id.launch_video_2);
				launch_video_2.setVisibility(View.VISIBLE);				break;
			case 1:
				launch_video_1 = (ImageButton) findViewById(R.id.launch_video_1);
				launch_video_1.setVisibility(View.VISIBLE);
				break;
			default:
				launch_video_1 = (ImageButton) findViewById(R.id.launch_video_1);
				launch_video_1.setVisibility(View.VISIBLE);
			}
			audioClips = mBuilding.audioClips;
			switch (audioClips.getCount()){
			case 3:
				launch_audio_1 = (ImageButton) findViewById(R.id.launch_audio_1);
				launch_audio_1.setVisibility(View.VISIBLE);
				launch_audio_2 = (ImageButton) findViewById(R.id.launch_audio_2);
				launch_audio_2.setVisibility(View.VISIBLE);
				launch_audio_3 = (ImageButton) findViewById(R.id.launch_audio_3);
				launch_audio_3.setVisibility(View.VISIBLE);
				break;
			case 2:
				launch_audio_1 = (ImageButton) findViewById(R.id.launch_audio_1);
				launch_audio_1.setVisibility(View.VISIBLE);
				launch_audio_2 = (ImageButton) findViewById(R.id.launch_audio_2);
				launch_audio_2.setVisibility(View.VISIBLE);				
				break;
			case 1:
				launch_audio_1 = (ImageButton) findViewById(R.id.launch_audio_1);
				launch_audio_1.setVisibility(View.VISIBLE);
				break;
			default:
				launch_audio_1 = (ImageButton) findViewById(R.id.launch_audio_1);
				launch_audio_1.setVisibility(View.VISIBLE);
			}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()){
		case R.id.launch_audio_1:
			intent = new Intent();
			intent.setAction(android.content.Intent.ACTION_VIEW);
			audioClips.moveToFirst();
			String audUri = audioClips.getString(audioClips.getColumnIndex(MyDBHelper.KEY_AUDIO_URI));
			intent.setData(Uri.parse(audUri));
			break;
		case R.id.launch_audio_2:
			intent = new Intent();
			intent.setAction(android.content.Intent.ACTION_VIEW);
			audioClips.moveToPosition(2);
			audUri = audioClips.getString(audioClips.getColumnIndex(MyDBHelper.KEY_AUDIO_URI));
			intent.setData(Uri.parse(audUri));
			break;
		case R.id.launch_audio_3:
			intent = new Intent();
			intent.setAction(android.content.Intent.ACTION_VIEW);
			audioClips.moveToPosition(3);
			audUri = audioClips.getString(audioClips.getColumnIndex(MyDBHelper.KEY_AUDIO_URI));
			intent.setData(Uri.parse(audUri));
			break;
		case R.id.launch_video_1:
			intent = new Intent();
			intent.setAction(android.content.Intent.ACTION_VIEW);
			audioClips.moveToFirst();
			String vidUri = videos.getString(videos.getColumnIndex(MyDBHelper.KEY_AUDIO_URI));
			intent.setData(Uri.parse(vidUri));
			break;
		case R.id.launch_video_2:
			intent = new Intent();
			intent.setAction(android.content.Intent.ACTION_VIEW);
			audioClips.moveToPosition(2);
			vidUri = videos.getString(videos.getColumnIndex(MyDBHelper.KEY_AUDIO_URI));
			intent.setData(Uri.parse(vidUri));
			break;
		case R.id.launch_video_3:
			intent = new Intent();
			intent.setAction(android.content.Intent.ACTION_VIEW);
			audioClips.moveToPosition(3);
			vidUri = videos.getString(videos.getColumnIndex(MyDBHelper.KEY_AUDIO_URI));
			intent.setData(Uri.parse(vidUri));
			break;
		case R.id.go_back_button:
			finish();
			break;
		}
		startActivity(intent); 
	}
}
